<?php $layout = 'admin';
$title = '用户管理 - MyCMS'; ?>

<div class="card">
    <div class="card-header">
        <h2>用户管理</h2>
    </div>
    <div class="card-body">
        <!-- 搜索表单 -->
        <form method="get" action="/admin/users" style="margin-bottom: 1.5rem;">
            <div style="display: flex; gap: 1rem; align-items: center;">
                <input type="text" name="kw" value="<?= htmlspecialchars($kw ?? '') ?>"
                    placeholder="搜索用户名或邮箱..."
                    style="flex: 1; padding: 0.75rem; border: 1px solid #ddd; border-radius: 4px; font-size: 1rem;">
                <button type="submit" class="btn">搜索</button>
                <?php if (!empty($kw)): ?>
                    <a href="/admin/users" class="btn btn-secondary">清除</a>
                <?php endif; ?>
            </div>
        </form>

        <!-- 用户列表 -->
        <?php if (empty($users)): ?>
            <div style="text-align: center; padding: 2rem 0;">
                <h3 style="color: #666; margin-bottom: 1rem;">暂无用户</h3>
                <p style="color: #999; margin-bottom: 2rem;">
                    <?php if (!empty($kw)): ?>
                        没有找到包含 "<?= htmlspecialchars($kw) ?>" 的用户
                    <?php else: ?>
                        系统中还没有注册用户
                    <?php endif; ?>
                </p>
            </div>
        <?php else: ?>
            <div style="margin-bottom: 1rem;">
                共 <?= $total ?> 位用户
            </div>
            <table class="table">
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>用户名</th>
                        <th>邮箱</th>
                        <th>注册时间</th>
                        <th>操作</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $user): ?>
                        <tr>
                            <td><?= $user['id'] ?></td>
                            <td><?= htmlspecialchars($user['username']) ?></td>
                            <td><?= htmlspecialchars($user['email']) ?></td>
                            <td><?= date('Y-m-d H:i', strtotime($user['created_at'])) ?></td>
                            <td>
                                <div style="display: flex; gap: 0.5rem;">
                                    <button class="btn btn-sm btn-secondary" disabled>编辑</button>
                                    <button class="btn btn-sm btn-danger" disabled>删除</button>
                                </div>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>

            <!-- 分页导航 -->
            <?php if ($totalPages > 1): ?>
                <div style="margin-top: 2rem; text-align: center;">
                    <div style="display: inline-flex; gap: 0.5rem; align-items: center;">
                        <?php
                        $currentPage = $page;
                        $baseUrl = '/admin/users';
                        $queryParams = [];
                        if (!empty($kw)) {
                            $queryParams['kw'] = $kw;
                        }

                        // 上一页
                        if ($currentPage > 1):
                            $prevParams = $queryParams;
                            $prevParams['page'] = $currentPage - 1;
                            $prevUrl = $baseUrl . '?' . http_build_query($prevParams);
                        ?>
                            <a href="<?= $prevUrl ?>" class="btn btn-sm btn-secondary">上一页</a>
                        <?php endif; ?>

                        <?php
                        // 页码显示逻辑
                        $startPage = max(1, $currentPage - 2);
                        $endPage = min($totalPages, $currentPage + 2);

                        // 如果开始页码大于1，显示第一页和省略号
                        if ($startPage > 1):
                            $firstParams = $queryParams;
                            $firstParams['page'] = 1;
                            $firstUrl = $baseUrl . '?' . http_build_query($firstParams);
                        ?>
                            <a href="<?= $firstUrl ?>" class="btn btn-sm btn-secondary">1</a>
                            <?php if ($startPage > 2): ?>
                                <span style="padding: 0.6rem; color: #666;">...</span>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php for ($i = $startPage; $i <= $endPage; $i++): ?>
                            <?php if ($i == $currentPage): ?>
                                <span class="btn btn-sm" style="background: #007bff; color: white;"><?= $i ?></span>
                            <?php else: ?>
                                <?php
                                $pageParams = $queryParams;
                                $pageParams['page'] = $i;
                                $pageUrl = $baseUrl . '?' . http_build_query($pageParams);
                                ?>
                                <a href="<?= $pageUrl ?>" class="btn btn-sm btn-secondary"><?= $i ?></a>
                            <?php endif; ?>
                        <?php endfor; ?>

                        <?php
                        // 如果结束页码小于总页数，显示省略号和最后一页
                        if ($endPage < $totalPages):
                            if ($endPage < $totalPages - 1):
                        ?>
                                <span style="padding: 0.6rem; color: #666;">...</span>
                            <?php endif; ?>
                            <?php
                            $lastParams = $queryParams;
                            $lastParams['page'] = $totalPages;
                            $lastUrl = $baseUrl . '?' . http_build_query($lastParams);
                            ?>
                            <a href="<?= $lastUrl ?>" class="btn btn-sm btn-secondary"><?= $totalPages ?></a>
                        <?php endif; ?>

                        <?php
                        // 下一页
                        if ($currentPage < $totalPages):
                            $nextParams = $queryParams;
                            $nextParams['page'] = $currentPage + 1;
                            $nextUrl = $baseUrl . '?' . http_build_query($nextParams);
                        ?>
                            <a href="<?= $nextUrl ?>" class="btn btn-sm btn-secondary">下一页</a>
                        <?php endif; ?>
                    </div>

                    <div style="margin-top: 1rem; color: #666; font-size: 0.9rem;">
                        第 <?= $currentPage ?> 页，共 <?= $totalPages ?> 页
                        <?php if (!empty($kw)): ?>
                            · 搜索结果
                        <?php endif; ?>
                    </div>
                </div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>